<?php
class ViewOffer extends AppModel {
	var $name = 'ViewOffer';
	var $useDbConfig = 'lex';
	var $displayField = 'id';
	var $validate = array();
	var $virtualFields = array(
				
			    'slowa_kluczowe' => 'CONCAT(ViewOffer.opis_publiczny, " ", ViewOffer.html," ",ViewOffer.lokalizacja," ",ViewOffer.dzielnica," ",ViewOffer.miasto," ",ViewOffer.ulica)'
	);
	public $actsAs = array('Searchable');
	
	public $filterArgs = array(
		array('name' => 'slowa_kluczowe', 'type' => 'like'),
		array('name' => 'typ_id', 'type' => 'value'),
		array('name' => 'transakcja_id', 'type' => 'value'),
		array('name' => 'rynek', 'type' => 'value'),
		array('name' => 'cena_od', 'type' => 'subquery', 'method' => 'findByCena', 'field' => 'ViewOffer.id'),
		array('name' => 'powierzchnia_od', 'type' => 'subquery', 'method' => 'findByPowierzchnia', 'field' => 'ViewOffer.id')
	);
	
	public function findByCena($data = array()){
		$this->Behaviors->attach('Containable', array('autoFields' => false));
		$this->Behaviors->attach('Search.Searchable');
		$data['cena_od'] = (empty($data['cena_od'])) ? 0 : $data['cena_od'];
		$data['cena_do'] = (empty($data['cena_do'])) ? 9999999 : $data['cena_do'];
		$query = $this->getQuery('all', array(
								'conditions' => array(
									"OR"=>
									array("ViewOffer.cena_od BETWEEN {$data['cena_od']} AND {$data['cena_do']}",
										  "ViewOffer.cena_do BETWEEN {$data['cena_od']} AND {$data['cena_do']}"
		)),
		'fields' => array('ViewOffer.id')
		));
		return $query;
		// 		return 0;
	}
	
	public function findByPowierzchnia($data = array()){
		
		$this->Behaviors->attach('Containable', array('autoFields' => false));
		$this->Behaviors->attach('Search.Searchable');
		$data['powierzchnia_od'] = (empty($data['powierzchnia_od'])) ? 0 : $data['powierzchnia_od'];
		$data['powierzchnia_do'] = (empty($data['powierzchnia_do'])) ? 9999 : $data['powierzchnia_do'];
		$query = $this->getQuery('all', array(
							'conditions' => array(
								"OR"=>
// 								array("{$data['powierzchnia_od']} BETWEEN ViewOffer.powierzchnia_od and ViewOffer.powierzchnia_do",
// 									  "{$data['powierzchnia_do']} BETWEEN ViewOffer.powierzchnia_od and ViewOffer.powierzchnia_do"
								array("ViewOffer.powierzchnia_od BETWEEN {$data['powierzchnia_od']} AND {$data['powierzchnia_od']}",
									  "ViewOffer.powierzchnia_do BETWEEN {$data['powierzchnia_od']} AND {$data['powierzchnia_do']}"
		
		)),
							'fields' => array('ViewOffer.id')
		));
		return $query;
		// 		return 0;
	}
}
